package com.xhapi.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhapi.pojo.dto.QADTO;
import com.xhapi.pojo.entity.ApiResult;
import com.xhapi.pojo.entity.HistoryQuestionByAi;
import com.xhapi.pojo.pageQuery.ElasticSearchPage;
import com.xhapi.pojo.pageQuery.QuestionAiPage;
import com.xhapi.pojo.vo.HistoryQuestionByAiVO;
import com.xhapi.service.ESService;
import com.xhapi.service.IHistoryQuestionByAiService;
import com.xhapi.utils.ApiResultHandler;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/history-question-by-ai")
@RequiredArgsConstructor
public class HistoryQuestionByAiController {
    private final IHistoryQuestionByAiService service;
    private final ESService esService;
    @ApiOperation("向知识库提问并获取结果，相关信息会存储在mysql中")
    @PostMapping
    public ApiResult<List<HistoryQuestionByAiVO>> createQA(@RequestBody QADTO qadto) {
        return ApiResultHandler.success(service.createQA(qadto));
    }

    @ApiOperation("提交答案：通过id获取题目，获取我的答案，通过大模型进行语义相似度比较获取得分，返回题目解析和正确答案、所给分数")
    @GetMapping
    public ApiResult analyzeQA(@RequestParam Integer id, @RequestParam String myAnswer) throws IOException {
        return ApiResultHandler.success(service.analyzeQA(id, myAnswer));
    }

    @ApiOperation("分页查询，ES版本")
    @GetMapping("/page")
    public ApiResult pageByEs(@RequestParam(name = "userId", required = false) String userId,
                              @RequestParam(name = "pageSize", defaultValue = "6") int pageSize,
                              @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                              @RequestParam(name = "subject", defaultValue = "地理") String subject,
                              @RequestParam(name = "desc", required = false) String desc) {
        ElasticSearchPage page=esService.pageByES(pageSize,pageNum,subject,desc);
        return ApiResultHandler.success(page);
    }
    @ApiOperation("更新题目信息，将MySQL重新写入ES中")
    @GetMapping("/updateES")
    public ApiResult updateES(){
        esService.updateES();
        return null;
    }

    @ApiOperation("根据id查询单个题目信息")
    @GetMapping("/{id}")
    public ApiResult getById(@PathVariable Integer id) {
        return ApiResultHandler.success(service.getById(id));
    }


    @ApiOperation("废弃的方法，分页查询所有AI生成过的题目，包含正确答案")
    @GetMapping("/page/abandon")
    public ApiResult page( @RequestParam(name = "userId", required = false) String userId,
                           @RequestParam(name = "pageSize", defaultValue = "6") int pageSize,
                           @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                           @RequestParam(name = "subject", required = false) String subject  ) {
        Page<HistoryQuestionByAi> result = service.lambdaQuery()
                .eq(StrUtil.isNotBlank(subject), HistoryQuestionByAi::getSubject, subject)
                .eq(HistoryQuestionByAi::getUserId, userId)
                .orderByAsc(HistoryQuestionByAi::getUpdateTime)
                .page(new Page(pageNum, pageSize));
        return ApiResultHandler.success(result);
    }
}
